﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using KPIS.DBM;
using KPIS.GERP.GIMS.BLL;
using KPIS.GERP.GIMS.MODEL;

namespace KPIS.GERP.GIMS.APP.APP_UI.Form
{
    public partial class Login : System.Web.UI.Page
    {
        UserBLL userBLL = new UserBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                switch (GetSystemName())
                {
                    case "DEP":
                    default:
                        headerImage.ImageUrl = "~/App_UI/Images/PageViews/Header.jpg";
                        break;
                    case "ITPF":
                        headerImage.ImageUrl = "~/App_UI/Images/PageViews/Header_ITPF.jpg";
                        break;
                }
                txtUserID.Focus();
            }
        }
        protected string GetSystemName()
        {
            IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);

            try
            {
                string strResult = "";
                string sql = "select system_name"
                    + " from sys_system_controls";
                DataTable dt = new DataTable();
                dbManager.Open();
                dt = dbManager.ExecuteDataSet(CommandType.Text, sql).Tables[0];

                if (dt.Rows.Count > 0)
                {
                    strResult = dt.Rows[0][0].ToString();
                }

                return strResult;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        protected void cmdLogin_Click(object sender, EventArgs e)
        {
            string name = txtUserID.Text.Trim();
            string password = txtPassword.Text.Trim();
            string UserStatus = "";

            DataRowCollection drc;
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(password))
            {
                RadManager1.Alert("กรุณากรอกรหัสผู้ใช้งานและรหัสผ่าน");
                return;
            }

            drc = userBLL.Login(name, password).Tables[0].Rows;

            if (drc.Count > 0)
            {
                UserStatus = drc[0]["user_status"].ToString();

                if (UserStatus == SystemInfo._RejectStatus)
                {
                    RadManager1.Alert("รอการตรวจสอบจากผู้ดูแลระบบ");
                    return;
                }
                else if (UserStatus == SystemInfo._ApproveRecordStatus)
                {
                    Session["EmpSeq"] = drc[0]["emp_seq"].ToString();
                    Session["emp_seq"] = drc[0]["emp_seq"].ToString();
                    Session["UserSeq"] = drc[0]["user_seq"].ToString();
                    Session["UserCode"] = drc[0]["user_code"].ToString();
                    Session["UserPass"] = drc[0]["user_password"].ToString();
                    Session["UserName"] = drc[0]["user_name"].ToString();
                    Session["Height"] = hHeight.Value; ;
                    Page.Response.Redirect("Default.aspx");
                }

            }
            else
            {
                RadManager1.Alert(SystemInfo._LoginFail);
                txtUserID.Focus();
            }
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            Response.Redirect("Login_Register_Form.aspx");
        }

        protected void btnForget_Click(object sender, EventArgs e)
        {
            Response.Redirect("Forget_Password.aspx");
        }

    }
}
